﻿using KAKA.Models;
using KAKA.Repository;
using Morus;
using System.Web.Mvc;
using System.Linq;
using KAKA.Domain;
using MvcContrib;

namespace KAKA.Controllers
{
    public class CategoryController : BaseController
    {
        public CategoryController()
            : base()
        {
            CategoryRepository = IoC.Resolve<ICategoryRepository>();
        }

        public ICategoryRepository CategoryRepository { get; private set; }

        [Administration]
        public ActionResult Index()
        {
            var categories = CategoryRepository.GetAll()
                .ToList()
                .Select(r => new CategoryModel()
                {
                    Id = r.Id.Shrink(),
                    Name = r.Name,
                    Description = r.Description,
                });

            return View(categories);
        }

        [Administration]
        public ActionResult Add()
        {
            return View();
        }

        [Administration]
        [ValidateAntiForgeryToken]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(CategoryModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var uow = UnitOfWork.Begin())
                    {
                        var category = Category.Create(model.Name);
                        category.Description = model.Description;

                        CategoryRepository.Add(category);

                        uow.Commit();
                    }
                }

                return this.RedirectToAction(r => r.Index());
            }
            catch
            {
                return View();
            }
        }

        [Administration]
        public ActionResult Edit(string id)
        {
            try
            {
                var category = CategoryRepository.Get(id.ToGuid());
                var model = new CategoryModel()
                {
                    Id = category.Id.Shrink(),
                    Name = category.Name,
                    Description = category.Description
                };

                return View(model);
            }
            catch
            {
                return Add();
            }
        }

        [Administration]
        [ValidateAntiForgeryToken]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(CategoryModel model)
        {
            try
            {
                using (var uow = UnitOfWork.Begin())
                {
                    var category = CategoryRepository.Get(model.Id.ToGuid());

                    category.Name = model.Name;
                    category.Description = model.Description;

                    uow.Commit();
                }

                return this.RedirectToAction(r => r.Index());
            }
            catch
            {
                return View();
            }
        }
    }
}
